class LoginController < ApplicationController

  layout "kogukond"

  before_filter :authorize, :except=> :login

  def login
    if request.get?
      session[:user_id] = nil
      @user = User.new
    else
      @user = User.new(params[:user])
      logged_in_user = @user.try_to_login
      if logged_in_user
        session[:user_id]       = logged_in_user.id
        session[:user_role]     = logged_in_user.role
        session[:user_name]     = logged_in_user.name
        session[:community_id]  = logged_in_user.community_id
        community = Community.find(:first, :conditions => ["id = ?", logged_in_user.community_id])
        session[:community_name] = community.name
        redirect_to(:action => "index")
      else
        flash[:notice] = "Invalid user/password combination"
        redirect_to(:action => "login")
      end
    end
  end

  def logout
    session[:user_id]         = nil
    session[:community_id]    = nil
    session[:user_role]       = nil
    session[:user_name]       = nil
    session[:community_name]  = nil
    flash[:notice] = "logged out"
    redirect_to(:action => "login")
  end

  def list_nodes
    redirect_to(:controller => "nodes", :action => "list")
  end

  def list_edges
    redirect_to(:controller => "edges", :action => "list")
  end

end
